package com.mes.check.manager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mes.check.manager.iface.IRejectDetailManager;
import com.mes.check.manager.iface.IRejectManager;
import com.mes.check.dao.RejectDao;
import com.mes.check.dao.iface.IDefectJudgeDetailDao;
import com.mes.check.dao.iface.IRejectDao;
import com.mes.check.domain.DefectJudgeDetail;
import com.mes.check.domain.Reject;
import com.mes.check.domain.RejectDetail;
import com.mes.common.manager.BasicManager;

@Component
public class RejectDetailManager extends BasicManager<RejectDetail> implements IRejectDetailManager {
	private final static Log log = LogFactory.getLog(RejectDetailManager.class);

	@Autowired
	private IRejectDao rejectDao;
	@Autowired
	private IDefectJudgeDetailDao defectJudgeDetailDao;

	public void CreateRejectDetail(String billUid) throws Exception {

		try {
			List<Reject> rejectList = (List<Reject>) rejectDao.findByProperty("defectJudgeUid", billUid);
			List<DefectJudgeDetail> defectJudgeDetailList = (List<DefectJudgeDetail>) defectJudgeDetailDao
					.findByProperty("defectJudgeUid", billUid);
			Integer seq = 0;
			if (rejectList.size() > 0) {
				for (Reject tempReject : rejectList) {
					if (defectJudgeDetailList.size() > 0) {
						for (DefectJudgeDetail tempDetail : defectJudgeDetailList) {
							if (tempDetail.getJudgeResult().equals("8")) {
								seq = seq + 1;
								RejectDetail rejectDetail = new RejectDetail();
								rejectDetail.setRejectUid(tempReject.getUuid());
								rejectDetail.setSeq(seq);
								rejectDetail.setUuid(tempReject.getUuid() + "-" + seq.toString());
								rejectDetail.setDefectDesc(tempDetail.getDefectDesc());
								rejectDetail.setRejectQty(tempDetail.getDefectQty());
								rejectDetail.setDutierUid(tempDetail.getDutierUid());
								rejectDetail.setDutyDeptUid(tempDetail.getDutyDeptUid());
								rejectDetail.setDutyDeptDelegate(tempDetail.getDutyDeptDelegate());
								rejectDetail.setDefectReason(tempDetail.getDefectReason());
								rejectDetail.setMaterialUid(tempDetail.getMaterialUid());
								this.add(rejectDetail);
								log.info("生成报废单明细！");
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

}
